﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Tedds_Tool
{
    public delegate void PropertyChangedEvent(Object sender, String property);

    public class Settings : ISettings
    {


        public DirectoryInfo projectRoot;
        public FileInfo testDLL;

        public DirectoryInfo ProjectRoot { 
            get { 
                return projectRoot; 
            } 
            set {
                DirectoryInfo gnu = value;
                if (!gnu.Equals(projectRoot))
                {
                    projectRoot = gnu;
                    if (OnPropertyChanged != null)
                        OnPropertyChanged(this, "ProjectRoot");
                }
                
            } 
        }
        public FileInfo TestDLL { 
            get { 
                return testDLL; 
            } 
            set {
                FileInfo gnu = value;
                if (!gnu.Equals(testDLL))
                {
                    testDLL = gnu;
                    if (OnPropertyChanged != null)
                        OnPropertyChanged(this, "TestDLL");
                }
            } 
        }
        
        public event PropertyChangedEvent OnPropertyChanged;

        public Settings() : this(null) {}
        public Settings(DirectoryInfo projectRoot)
        {
            OnPropertyChanged += Load;
            if (projectRoot != null)
            {
                ProjectRoot = projectRoot;
            }
            OnPropertyChanged += PropertyChanged;
        }

        public void Load(Object sender, String property)
        {
            if ("ProjectRoot".Equals(property))
            {
                FileInfo config = new FileInfo(ProjectRoot.FullName + "/LEET.config");
                if (config.Exists)
                {
                    TextReader reader = new StreamReader(config.FullName, Encoding.ASCII);
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        string[] args = line.Split('|');
                        switch (args[0])
                        {
                            case "TestDLL": TestDLL = new FileInfo(args[1]); break;
                        }

                    }
                }
            }
        }

        private void PropertyChanged(Object sender, String property){
//            Save();
        }

        public override String ToString()
        {
            return new StringBuilder()
            //  .Append(<name>).Append(<seperator>).Append(<value>).Append(<newLine>)
                .Append("TestDLL").Append("|").Append(TestDLL.FullName).Append("\r\n")
                .ToString();
        }

    }
}
